<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<table border='1' class="html-tabel">
    <tr style="text-align: center;">
        <th rowspan="2" style="background:red;">日期</th>
        <th rowspan="2" style="background:red;">姓名</th>
        <th colspan="3" style="background:red;">地址</th>
    </tr>
    <tr style="text-align: center;">
        <th style="background:red;">省份</th>
        <th style="background:red;">市区</th>
        <th style="background:red;">地址</th>
    </tr>
    <tr style="text-align: center;">
        <td>2020-05-20</td>
        <td>张三</td>
        <td>上海</td>
        <td>普陀区</td>
        <td>上海市普陀区金沙江路 1518 弄</td>
    </tr>
    <tr style="text-align: center;">
        <td>2020-11-11</td>
        <td>李四</td>
        <td>上海</td>
        <td>普陀区</td>
        <td>上海市普陀区金沙江路 1518 弄</td>
    </tr>
    </table>
        <br />
        <button onclick="tableToExcel()">导出excel</button>

</body>
<script>
    function tableToExcel() {
        var tabelStr = document.querySelector('.html-tabel').outerHTML;
        //Worksheet名
        var worksheet = '测试表名'
        var uri = 'data:application/vnd.ms-excel;base64,';
        // 真正要导出（下载）的HTML模板
        var exportTemplate = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" '+
            'xmlns="http://www.w3.org/TR/REC-html40"><head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet> <x:Name>${worksheet}</x:Name> <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet> </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--> </head> <body>${tabelStr} </body> </html>';
        //下载模板
        var a = document.createElement("a");
        a.download = worksheet + ".xlsx";
        a.href = uri + base64(exportTemplate);
        a.click();
    };
    //输出base64编码
    function base64(s) {
        return window.btoa(unescape(encodeURIComponent(s)))
    };
</script>
</html>